/*
 * web 2.0 MBDS 2012 Michel Buffa
 */
package com.vampmobile.android.tools;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;

import com.vampmobile.R;
import com.vampmobile.android.VampApplication;
import com.vampmobile.android.activity.Login;
import com.vampmobile.android.activity.PoiDetail;
import com.vampmobile.android.activity.VampNFCControl;
import com.vampmobile.android.db.VampREST;

/*
 * Class to create menu
 * FO && AL
 */
public class ApplicationMenu {
	public static void onCreateOptionsMenu(Activity context, Menu menu) {
		MenuInflater inflater = context.getMenuInflater();
		inflater.inflate(R.menu.menu, menu);
	}

	public static void onPrepareOptionsMenu(Activity context, Menu menu) {
		boolean authenticated = VampREST.isAuthenticated();

		if (authenticated) {
			boolean online = VampApplication.getInstance().isOnline();
			menu.findItem(R.id.startNFC).setEnabled(online);
		}

		menu.findItem(R.id.delete).setVisible(true);
		menu.findItem(R.id.logout).setVisible(authenticated);

		// the menu Hit it visible just for the user object in the case screen
		// of object detail
		if (context.getTitle().toString().equalsIgnoreCase(PoiDetail.TAG)) {
			menu.findItem(R.id.startNFC).setVisible(true);
		}
		Log.i("le contexte de la methode onPrepareOptionsMenu ", context
				.getTitle().toString());
	}

	public static void onOptionsItemSelected(Activity context, MenuItem item) {
		switch (item.getItemId()) {
		case R.id.startNFC:
			Intent i = new Intent(context, VampNFCControl.class);
			context.startActivity(i);
			break;

		case R.id.logout:
			// Clear passwords and other settings
			context.getSharedPreferences(VampApplication.PREFS,
					Context.MODE_PRIVATE).edit().clear().commit();
			VampREST.setBasicAuthentication(null, null);
			ActivityParameters.init();
			Intent login = new Intent(context, Login.class);
			context.startActivity(login);

			context.finish();
			break;
		}
	}
}
